<?php

declare(strict_types=1);

namespace App\WorkContact\Controller\Dashboard\TagGroup;

use App\WorkContact\Service\WorkContactTagGroupService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Common\Middleware\DashboardAuthMiddleware;
use App\WorkContact\Service\WorkContactTagService;
use Base\Action\AbstractAction;

/**
 * 所有标签.
 *
 * Class AllTag
 * @Controller
 */
class AllTag extends AbstractAction
{
    /**
     * @Inject
     * @var WorkContactTagGroupService
     */
    private $contactTagGroupService;

    /**
     * @Inject
     * @var WorkContactTagService
     */
    private $contactTagService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/workContactTagGroup/allGroupTag", methods="GET")
     */
    public function handle(): array
    {
        $corpIds[0] = $this->request->input('userInfo.corp_id');

        //获取所有标签
        $tagGroupsInfo = $this->contactTagGroupService
            ->getWorkContactTagGroupsByCorpId($corpIds, ['*']);
        $tagGroupsTreeInfo = [];
        if (empty($tagGroupsInfo)) {
            return [];
        }else{
            foreach ($tagGroupsInfo as $item => $value){
                $tagGroupsTreeInfo[$item]['groupId'] = $value['id']??'';
                $tagGroupsTreeInfo[$item]['groupName'] = $value['groupName']??'';
                $contactTag = [];
                if(!empty($value['id'])) $contactTag = $this->contactTagService->getWorkContactTagsByCorpIdGroupIds($corpIds,[$value['id']],['id','wx_contact_tag_id','name']);
                if(!empty($contactTag)) $tagGroupsTreeInfo[$item]['contactTags'] = $contactTag;
            }
        }

        return $tagGroupsTreeInfo;
    }
}
